package wang.hu.crm.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import wang.hu.crm.dao.CustomerDao;
import wang.hu.crm.domain.Customer;
import wang.hu.crm.service.CustomerService;

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
	@Autowired
	private CustomerDao customerDao;

	@Override
	public List<Customer> findNoLinkedCustomers() {
		// TODO Auto-generated method stub
		return customerDao.findByFixedAreaIdIsNull();
	}

	@Override
	public List<Customer> findHasLinkedCustomers(String fixedAreaId) {
		// TODO Auto-generated method stub
		return customerDao.findByFixedAreaId(fixedAreaId);
	}

	@Override
	public void linkedCustomersToFixedArea(String customerIdStr,
			String fixedAreaId) {
		customerDao.clearFixedAreaId(fixedAreaId);
		if (StringUtils.isBlank(customerIdStr)) {
			System.out.println("客户id串为空");
			return;
		}
		String[] ids = customerIdStr.split(",");
		for (String idstr : ids) {
			int id = Integer.parseInt(idstr);
			customerDao.updateFixedAreaId(fixedAreaId, id);
		}

	}

	@Override
	public String saveCustomer(Customer customer) {
		customerDao.save(customer);
		return null;
	}

	@Override
	public Customer findByTelephone(String telephone) {
		return customerDao.findByTelephone(telephone);
	}

	@Override
	public void setType(String telephone) {
		customerDao.updateType(telephone);

	}

	@Override
	public Customer login(String telephone, String password) {
		return customerDao.findByTelephoneAndPassword(telephone, password);
	}

	@Override
	public String findFixedAreaIdByAddress(String address) {
		return customerDao.findFixedAreaIdByAddress(address);
	}

}
